﻿using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using RandomSearch.Host;
using RandomSearch.PluginsCommon;

namespace ManualTests {
    internal class Program {
        private static void Main(string[] args) {
            WinFolderSearch(new RandomSearchServer());
        }


        private static void WinFolderSearch(ISearchServer server) {
            var searchCanceled = new ManualResetEvent(false);
            var t = new Task(() =>
                                 {
                                     var o = new SearchOptions
                                                 {
                                                     IncludeSubfolders = true,
                                                     PathToRootFolder = "C:\\Windows",
                                                     RequiredAttributes = FileAttributes.ReadOnly,
                                                     PluginId = "Default"
                                                 };
                                     server.StartSearch(o);
                                 });

            var t2 = new Task(() =>
                                  {
                                      while (!searchCanceled.WaitOne(TimeSpan.FromSeconds(0.5d))) {
                                          ReadyResultsResponse result = server.GetReadyResults();

                                          if (result.InProgress && null != result.Results) {
                                              foreach (SearchResult searchResult in result.Results)
                                                  Console.WriteLine(searchResult.File);
                                              continue;
                                          }
                                          if (!result.InProgress && null != result.Error) {
                                              Console.WriteLine(result.Error.Message);
                                              searchCanceled.Set();
                                              continue;
                                          }
                                          searchCanceled.Set();
                                      }
                                      Console.WriteLine("Done!");
                                  });

            t.Start();
            t2.Start();

            Console.WriteLine("Press enter for canceling...");
            Console.ReadLine();
        }
    }
}